﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TestApp
{
    public partial class Page1 : UserControl
    {

        MouseEventHandler meh;
        Point MouseDownAt;
        double img_actualWidth = 0;
        double img_actualHeight = 0;
		public Page1()
		{
            meh = new MouseEventHandler(img_c_MouseMove);
			InitializeComponent();
            bar.Value = 0;
            Uri uri = new Uri("image/1.jpg",UriKind.RelativeOrAbsolute);
            BitmapImage bitmap = new BitmapImage();
            bitmap.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitmap_DowLoad);
            bitmap.UriSource = uri;
            img_c.Source = bitmap;
		}

        private void bitmap_DowLoad(object sender, DownloadProgressEventArgs e)
        {
            bar.Value = (double)e.Progress;
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            img_c.MouseMove += meh;
            MouseDownAt = e.GetPosition(null);
            img_c.CaptureMouse();
        }
        void img_c_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(null);
            img_Scroll.ScrollToVerticalOffset(img_Scroll.VerticalOffset - ((p.Y - MouseDownAt.Y) * 1.2));
            img_Scroll.ScrollToHorizontalOffset(img_Scroll.HorizontalOffset - ((p.X - MouseDownAt.X) * 1.2));
            MouseDownAt = p;
        }
        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            img_c.MouseMove -= meh;
            img_c.ReleaseMouseCapture();
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (img_scale != null)
            {
                string t = e.NewValue.ToString();

                img_c.Width = (zoom_slider.Value / 100) * img_actualWidth;
                img_c.Height = (zoom_slider.Value / 100) * img_actualHeight;



                string leftT = "";
                string rightT = "";
                int index = t.IndexOf('.');
                if (index != -1)
                {
                    leftT = t.Substring(0, index);
                    rightT = t.Substring(index + 1, t.Length - index - 1);
                    if (rightT.Length > 1)
                    {
                        rightT = rightT.Substring(0, 1);
                    }
                }
                else
                {
                    leftT = t;
                    rightT = "0";
                }
                if (leftT.Length == 1)
                    leftT = "0" + leftT;
                percent.Text = leftT + "." + rightT + "%";
            }
        }
        private void img_c_Loaded(object sender, RoutedEventArgs e)
        {
            percent.Text = "100%";
            zoom_slider.Value = 100;
            Stretch s = img_c.Stretch;
            img_c.Stretch = Stretch.None;
            img_actualHeight = img_c.ActualHeight;
            img_actualWidth = img_c.ActualWidth;
            img_c.Stretch = s;
        }
	}
}
